
wndLogin = {
    posInit: false,
    localStorage: null,
    oldw: 0,

    /* window events */

    onWindowLoad: function(obj) {
        var name = document.getElementById('txt_name');
        name.focus();

        this.localStorage = new UCLocalStorage();
        this.localStorage.open();

        name.value = this.localStorage.getItem('lastUserName');
        name.setSelectionRange(name.value.length, name.value.length);
        if (name.value != '')
            document.getElementById('txt_password').focus();
        
        document.getElementById('txt_dataserver').value = this.localStorage.getItem('dataServer');
        document.getElementById('txt_statusserver').value = this.localStorage.getItem('statusServer');
    },

    onWindowResize: function(obj) {
        this.setWindowToCenterScreen(obj);
    },

    onWindowClose: function(obj) {
        closeStorage();
    },

    /* help functions */

    setWindowToCenterScreen: function(obj) {
        // выравниваю окно по центру экрана
        if (!this.posInit) {
            obj.moveTo((obj.screen.width - obj.innerWidth) / 2, (obj.screen.height - obj.innerHeight) / 2);
            this.posInit = true;
            this.oldw = obj.innerWidth;
        }
    },

    closeStorage: function() {
        this.localStorage.close();
        delete this.localStorage;
    },

    /* commands */

    onCommandSettings: function(obj) {
        var grp_settings = document.getElementById('grp_settings');
        var btn_settings = document.getElementById('btn_settings');
        
        if (!this.tmpCaption) this.tmpCaption = btn_settings.label;
        
        if (grp_settings.getAttribute('hidden') == "true") {
            window.resizeBy(300, 0);
            grp_settings.setAttribute('hidden', 'false');
        } else {
            grp_settings.setAttribute('hidden', 'true');
            window.resizeBy(-300, 0);
            // так и не понял почему не возвращается к исходному размеру
            // поэтому пришлось прибегнуть к такой хитрости/глупости ;-(
            window.resizeBy((window.innerWidth - this.oldw) * -1, 0);

            btn_settings.label = this.tmpCaption;
        }
    },

    onCommandCancel: function(obj) {
        this.closeStorage();
        window.close();
    },

    onCommandOk: function(obj) {
        this.onCommandApply(obj);
        // TODO: обновлять только в случае удачного входа???
        this.localStorage.setItem('lastUserName', document.getElementById('txt_name').value);
        // TODO: здесь должна быть авторизация
        window.open("chrome://unicon/content/main.xul", "unicon", "chrome,resizable=yes");
        window.close();
    },

    onCommandApply: function(obj) {
        this.localStorage.setItem('dataServer', document.getElementById('txt_dataserver').value);
        this.localStorage.setItem('statusServer', document.getElementById('txt_statusserver').value);
    }   
};